package com.seedeer.framework.i18n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * i18n工具类
 *
 * @author wangqiang
 **/
@Component
public class I18nUtil {

    @Autowired
    private MessageSource messageSource;


    private static I18nUtil i18nUtil;

    @PostConstruct
    public void init() {
        i18nUtil = this;
        i18nUtil.messageSource = this.messageSource;
    }

    /**
     * 统多语言翻译
     *
     * @return java.lang.String
     * @Author wangqiang
     * @Date 2018/7/20 13:47
     * @Param code
     */
    public static String getMessage(String code) {
        return i18nUtil.messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
    }

    /**
     * @return java.lang.String
     * @Description 系统多语言翻译, 加参数
     * @Author wangqiang
     * @Date 2018/7/20 13:47
     * @Param [code, args]
     */
    public static String getMessage(String code, Object... args) {
        return i18nUtil.messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
    }
}
